package com.ored.userWeb;

import com.ored.entity.User;
import com.ored.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author SGM
 * @Date 2020/3/16 8:40
 * @Version V1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UsersService usersService;

    @RequestMapping("/index")
    public String index() {
        return "user/addUser";
    }

    /**
     * @return String
     * @Description:页面跳转
     * @Date 11:07 2020/3/18
     * @Param String
     **/
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page) {
        return page;
    }


    /**
     * @Description:添加用户
     * @Date 15:33 2020/3/19
     * @Param:user
     **/
    @RequestMapping("/addUser")
    public String addUser(User user, Model model) {
        log.info("-------addUser start---------");

        int count = usersService.addUser(user);

        log.info("-------插入" + count + "条信息,数据为：" + user.toString());
        log.info("-------addUser end---------");

        return "redirect:hello";
    }


    @RequestMapping("/hello")
    public String showUser(Model model) {
        List<User> list = new ArrayList<>();

//        list.add(new User("001", "张三", "pw1230", 1));
//        list.add(new User("002", "张三", "pw1230", 1));
//        list.add(new User("003", "张三", "pw1230", 1));

        list = usersService.selectAllUsers();

        System.out.println("----------------------------");
        model.addAttribute("userslist", list);
        return "first";
    }


}
